Imports System.Data.SqlClient


Public Class cls_cuota_

    Implements ICloneable

#Region "Variables ..."
    Private int_idcuota As Integer = 0
    Private int_idpoliza As Integer = 0
    Private int_numero As Integer = 0
    Private int_total As Integer = 0
    Private dbl_importe As Decimal = 0
    Private dte_vencimiento As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private dte_vencimientoR As DateTime = New System.DateTime(Now.Year, 1, 1)
    Private str_Obs As String = ""
    Private str_Observaciones As String = ""
    Private int_idcliente As Integer = 0

    Private obj_poliza As cls_poliza_ = Nothing

    Private col_Items As Collections.Generic.List(Of cls_cuota_) = Nothing
#End Region

#Region "Propiedades ..."
    Property idcuota() As Integer
        Get
            idcuota = int_idcuota
        End Get
        Set(ByVal value As Integer)
            int_idcuota = value
        End Set
    End Property
    Property idpoliza() As Integer
        Get
            idpoliza = int_idpoliza
        End Get
        Set(ByVal value As Integer)
            int_idpoliza = value
        End Set
    End Property
    Property numero() As Integer
        Get
            numero = int_numero
        End Get
        Set(ByVal value As Integer)
            int_numero = value
        End Set
    End Property
    Property total() As Integer
        Get
            total = int_total
        End Get
        Set(ByVal value As Integer)
            int_total = value
        End Set
    End Property
    Property importe() As Decimal
        Get
            importe = dbl_importe
        End Get
        Set(ByVal value As Decimal)
            dbl_importe = value
        End Set
    End Property
    Property vencimiento() As DateTime
        Get
            vencimiento = dte_vencimiento
        End Get
        Set(ByVal value As DateTime)
            dte_vencimiento = value
        End Set
    End Property
    Property vencimientoR() As DateTime
        Get
            vencimientoR = dte_vencimientoR
        End Get
        Set(ByVal value As DateTime)
            dte_vencimientoR = value
        End Set
    End Property
    Property Obs() As String
        Get
            Obs = str_Obs
        End Get
        Set(ByVal value As String)
            str_Obs = value
        End Set
    End Property
    Property Observaciones() As String
        Get
            Observaciones = str_Observaciones
        End Get
        Set(ByVal value As String)
            str_Observaciones = value
        End Set
    End Property
    Property idcliente() As Integer
        Get
            idcliente = int_idcliente
        End Get
        Set(ByVal value As Integer)
            int_idcliente = value
        End Set
    End Property
    Property poliza As cls_poliza_
        Get
            If IsNothing(obj_poliza) Then
                obj_poliza = New cls_poliza_
                If Not obj_poliza.Buscar(idpoliza) Then
                    obj_poliza = Nothing
                End If
            End If
            poliza = obj_poliza
        End Get
        Set(ByVal value As cls_poliza_)
            obj_poliza = value
        End Set
    End Property
    Property Items() As Collections.Generic.List(Of cls_cuota_)
        Get
            Items = col_Items
        End Get
        Set(ByVal value As Collections.Generic.List(Of cls_cuota_))
            col_Items = value
        End Set
    End Property
#End Region

    Private Sub CargarColeccion(ByVal obj_Reader As SqlDataReader)
        col_Items = New Collections.Generic.List(Of cls_cuota_)
        While obj_Reader.Read()
            Dim Elemento As New cls_cuota_
            Elemento.int_idcuota = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcuota")))

            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idpoliza"))) = False Then
                Elemento.int_idpoliza = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idpoliza")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("numero"))) = False Then
                Elemento.int_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("numero")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("total"))) = False Then
                Elemento.int_total = (obj_Reader.GetValue(obj_Reader.GetOrdinal("total")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
                Elemento.dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento"))) = False Then
                Elemento.dte_vencimiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoR"))) = False Then
                Elemento.dte_vencimientoR = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoR")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("Obs"))) = False Then
                Elemento.str_Obs = (obj_Reader.GetValue(obj_Reader.GetOrdinal("Obs")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("Observaciones"))) = False Then
                Elemento.str_Observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("Observaciones")))
            End If
            If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente"))) = False Then
                Elemento.int_idcliente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente")))
            End If
            col_Items.Add(Elemento)
        End While
    End Sub

    Private Sub CargarVariables(ByVal obj_Reader As SqlDataReader)
        obj_Reader.Read()
        int_idcuota = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcuota")))

        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idpoliza"))) = False Then
            int_idpoliza = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idpoliza")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("numero"))) = False Then
            int_numero = (obj_Reader.GetValue(obj_Reader.GetOrdinal("numero")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("total"))) = False Then
            int_total = (obj_Reader.GetValue(obj_Reader.GetOrdinal("total")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("importe"))) = False Then
            dbl_importe = (obj_Reader.GetValue(obj_Reader.GetOrdinal("importe")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento"))) = False Then
            dte_vencimiento = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimiento")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoR"))) = False Then
            dte_vencimientoR = (obj_Reader.GetValue(obj_Reader.GetOrdinal("vencimientoR")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("Obs"))) = False Then
            str_Obs = (obj_Reader.GetValue(obj_Reader.GetOrdinal("Obs")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("Observaciones"))) = False Then
            str_Observaciones = (obj_Reader.GetValue(obj_Reader.GetOrdinal("Observaciones")))
        End If
        If IsDBNull(obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente"))) = False Then
            int_idcliente = (obj_Reader.GetValue(obj_Reader.GetOrdinal("idcliente")))
        End If
    End Sub

    Public Sub Nuevo(Optional ByVal ObtenerID As Boolean = False)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__Nuevo"
                .Parameters.Add("@idpoliza", SqlDbType.Int).Value = int_idpoliza
                .Parameters.Add("@numero", SqlDbType.Int).Value = int_numero
                .Parameters.Add("@total", SqlDbType.Int).Value = int_total
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .Parameters.Add("@vencimiento", SqlDbType.DateTime).Value = dte_vencimiento
                .Parameters.Add("@vencimientoR", SqlDbType.DateTime).Value = dte_vencimientoR
                .Parameters.Add("@Obs", SqlDbType.NVarChar).Value = str_Obs
                .Parameters.Add("@Observaciones", SqlDbType.NVarChar).Value = str_Observaciones
                .Parameters.Add("@idcliente", SqlDbType.Int).Value = int_idcliente
                .ExecuteNonQuery()
            End With
            If ObtenerID Then
                CargarUltimo()
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Crear el registro de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Modificar()
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__Modificar"
                .Parameters.Add("@idcuota", SqlDbType.Int).Value = int_idcuota
                .Parameters.Add("@idpoliza", SqlDbType.Int).Value = int_idpoliza
                .Parameters.Add("@numero", SqlDbType.Int).Value = int_numero
                .Parameters.Add("@total", SqlDbType.Int).Value = int_total
                .Parameters.Add("@importe", SqlDbType.Decimal).Value = dbl_importe
                .Parameters.Add("@vencimiento", SqlDbType.DateTime).Value = dte_vencimiento
                .Parameters.Add("@vencimientoR", SqlDbType.DateTime).Value = dte_vencimientoR
                .Parameters.Add("@Obs", SqlDbType.NVarChar).Value = str_Obs
                .Parameters.Add("@Observaciones", SqlDbType.NVarChar).Value = str_Observaciones
                .Parameters.Add("@idcliente", SqlDbType.Int).Value = int_idcliente
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Modificar el registro de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Sub Eliminar(ByVal idcuota As Integer)
        Dim obj_Conexion As New cls_Conexion
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__Eliminar"
                .Parameters.Add("@idcuota", SqlDbType.Int).Value = idcuota
                .ExecuteNonQuery()
            End With
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Eliminar el registro de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
        End Try
    End Sub

    Public Function Buscar(ByVal idcuota As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            Buscar = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__Buscar"
                .Parameters.Add("@idcuota", SqlDbType.Int).Value = idcuota
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
                Buscar = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al Buscar el registro de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function

    Public Sub CargarUltimo()
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__CargarUltimo"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarVariables(obj_Reader)
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarUltimo registro de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Sub

    Public Function CargarTodos() As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarTodos = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__CargarTodos"
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarTodos = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function CargarXidpoliza(ByVal prm_idpoliza As Integer) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXidpoliza = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__CargarXidpoliza"
                .Parameters.Add("@idpoliza", SqlDbType.Int).Value = prm_idpoliza
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXidpoliza = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarXidpoliza los registros de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function CargarXFecha(ByVal dte_desde As DateTime, ByVal dte_hasta As DateTime) As Boolean
        Dim obj_Conexion As New cls_Conexion
        Dim obj_Reader As SqlDataReader
        Try
            CargarXFecha = False
            obj_Conexion.Conectar()
            With obj_Conexion.obj_SqlCommand
                .CommandType = CommandType.StoredProcedure
                .Connection = obj_Conexion.obj_SqlConnection
                .CommandText = "cuota__CargarXFecha"
                .Parameters.Add("@desde", SqlDbType.DateTime).Value = dte_desde
                .Parameters.Add("@hasta", SqlDbType.DateTime).Value = dte_hasta
            End With
            obj_Reader = obj_Conexion.obj_SqlCommand.ExecuteReader
            If obj_Reader.HasRows Then
                CargarColeccion(obj_Reader)
                CargarXFecha = True
            End If
        Catch ex As SqlClient.SqlException
            MessageBox.Show("Error al CargarTodos los registros de cuota_." & Chr(13) & _
            "Por favor informe del siguiente detalle técnico:" & Chr(13) & _
            "Nro de error: " & ex.ErrorCode & "." & Chr(13) & _
            "Descripción: " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            obj_Conexion.Desconectar()
            obj_Conexion = Nothing
            obj_Reader = Nothing
        End Try
    End Function
    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj_cuota_Clon As New cls_cuota_
        obj_cuota_Clon.int_idcuota = Me.int_idcuota
        obj_cuota_Clon.int_idpoliza = Me.int_idpoliza
        obj_cuota_Clon.int_numero = Me.int_numero
        obj_cuota_Clon.int_total = Me.int_total
        obj_cuota_Clon.dbl_importe = Me.dbl_importe
        obj_cuota_Clon.dte_vencimiento = Me.dte_vencimiento
        obj_cuota_Clon.dte_vencimientoR = Me.dte_vencimientoR
        obj_cuota_Clon.str_Obs = Me.str_Obs
        obj_cuota_Clon.str_Observaciones = Me.str_Observaciones
        obj_cuota_Clon.int_idcliente = Me.int_idcliente
        obj_cuota_Clon.col_Items = Me.col_Items
        Return obj_cuota_Clon
    End Function

End Class
